package com.msti.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.msti.entity.Batch;
import com.msti.entity.Course;
import com.msti.entity.Student;
import com.msti.entity.StudentBatchCourse;
import com.msti.service.BatchMgtDAO;
import com.msti.service.BatchMgtDAOImpl;
import com.msti.service.CourseMgtDAO;
import com.msti.service.CourseMgtDAOImpl;
import com.msti.service.StudentBatchCourseMgtDAO;
import com.msti.service.StudentBatchCourseMgtDAOImpl;

/**
 * Servlet implementation class StudentDetail2
 */
public class StudentDetail2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentDetail2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] course = request.getParameterValues("course");
		CourseMgtDAO courseMgtDAO = new CourseMgtDAOImpl();
		BatchMgtDAO batchMgtDAO = new BatchMgtDAOImpl();
		StudentBatchCourseMgtDAO studentBatchCourseMgtDAO = new StudentBatchCourseMgtDAOImpl();
		StudentBatchCourse studentBatchCourse = new StudentBatchCourse();
		String stdudentId = request.getParameter("studentId");
		for(String cId : course){
			Course c = courseMgtDAO.getCourseById(Long.parseLong(cId));
			Batch b = batchMgtDAO.getBatchByCourseId(c.getId());
			studentBatchCourse.setBatchId(b.getId());
			studentBatchCourse.setStudentID(stdudentId);
			studentBatchCourse.setCourseId(b.getCourse_id());
			studentBatchCourseMgtDAO.insert(studentBatchCourse);
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//request.getSession().setAttribute("courses", course);
		//Student student = (Student)request.getSession().getAttribute("student");
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("capture.jsp");
		//load batches and redirect courses which are mapping to the batch id
		requestDispatcher.forward(request, response);

	}

}
